using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Primitivas3D
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        KeyboardState teclado_atual;
        VertexPositionColor[] primitivas;
        Matrix rotacao;
        Matrix translacao;
        Matrix scale;
        BasicEffect efeitoBasico;

        Vector3 camera;
        Vector3 cameravisao;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            efeitoBasico = new BasicEffect(GraphicsDevice);
            cameravisao = new Vector3(0, 0, 0);
            camera = new Vector3(0, 0, 3);
            rotacao = Matrix.CreateRotationZ(MathHelper.ToRadians(90));
            
            primitivas = new VertexPositionColor[3];

            primitivas[0] = new VertexPositionColor();
            primitivas[0].Position = new Vector3(0, 1, 0);
            primitivas[0].Color = Color.Red;

            primitivas[1] = new VertexPositionColor();
            primitivas[1].Position = new Vector3(1, -1, 0);
            primitivas[1].Color = Color.Blue;

            primitivas[2] = new VertexPositionColor();
            primitivas[2].Position = new Vector3(-1, -1, 0);
            primitivas[2].Color = Color.Green;

            efeitoBasico.World *= Matrix.CreateScale(0.3f);
            efeitoBasico.World *= Matrix.CreateRotationZ(MathHelper.ToRadians(90));
            efeitoBasico.World *= Matrix.CreateTranslation(new Vector3(1, 1, 0));
            
            efeitoBasico.Projection = Matrix.CreatePerspectiveFieldOfView(
                MathHelper.PiOver4,
                graphics.GraphicsDevice.Viewport.AspectRatio,
                0.1f,
                100.0f);


            efeitoBasico.VertexColorEnabled = true;

            

            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            
            efeitoBasico.View = Matrix.CreateLookAt(
                camera,
                cameravisao,
                Vector3.Up);
            
               

            teclado_atual = Keyboard.GetState();
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            
            // TODO: Add your update logic here
            
            if (teclado_atual.IsKeyDown(Keys.Left))
            {
                camera.X--;
                cameravisao.X--;
                
            }

            if (teclado_atual.IsKeyDown(Keys.Right))
            {
                camera.X++;
                cameravisao.X++;
            }

            if (teclado_atual.IsKeyDown(Keys.PageUp))
            {
                camera.Z--;
                cameravisao.Z--;
            }

            if (teclado_atual.IsKeyDown(Keys.PageDown))
            {
                camera.Z++;
                cameravisao.Z++;
            }

            if (teclado_atual.IsKeyDown(Keys.Up))
            {
                camera.Y++;
                cameravisao.Y++;
            }

            if (teclado_atual.IsKeyDown(Keys.Down))
            {
                camera.Y--;
                cameravisao.Y--;
            }

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Purple);


            efeitoBasico.CurrentTechnique.Passes[0].Apply();

            GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(
                PrimitiveType.TriangleStrip,
                primitivas,
                0,
                1);
            // TODO: Add your drawing code here

            base.Draw(gameTime);
        }
    }
}
